﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SimpleChessEmptySquare : Actors
{
	public SpriteRenderer SRenderer {get { return m_SRenderer;} set { m_SRenderer = value;}}

	private SpriteRenderer m_SRenderer;
	private Color m_InitialColor;
	public Color InitialColor
	{
		get { return m_InitialColor; }
	}

	private PawnData m_CurrentPawn;
	public PawnData CurrentPawn
	{
		get { return m_CurrentPawn; }
		set { m_CurrentPawn = value; }
	}

	private bool m_MouseIsIn;
	public bool MouseIsIn
	{
		get { return m_MouseIsIn; }
	}
		
	private int m_Position;
	public int Position
	{
		get { return m_Position; }
	}

	private void Awake () 
	{
	}
	
	private void Start () 
	{
		m_SRenderer = GetComponent<SpriteRenderer>();
		m_InitialColor = m_SRenderer.material.color;
	}

	private void Update ()
	{
		CheckMouseOver();
	}

	private void OnMouseEnter()
	{
		m_MouseIsIn = true;
		SimpleChessGame.Game.HoveredSquare = this;
	}

	private void OnMouseExit()
	{
		m_MouseIsIn = false;
		SimpleChessGame.Game.HoveredSquare = null;
	}

	private void CheckMouseOver()
	{
		if(m_MouseIsIn)
		{
			if(UIFlowManager.Instance.UIFlowReady && m_CurrentPawn != null)
			{
				if(	SimpleChessGame.Game.SelectedPawn == null				&&
				   	m_CurrentPawn != null									&&
				   	SimpleChessGame.Game.HoveredSquare.CurrentPawn != null	)
				{
					// Check if the hovered pawn is a pawn of the current player
					if(SimpleChessGame.Game.HoveredSquare.CurrentPawn.PlayerOwner == SimpleChessGame.Game.CurrentTurn )
					{
						m_CurrentPawn.EnablePlayerScale = true;
					}
					else
					{
						m_CurrentPawn.EnablePlayerIcon = true;
					}
				}
			}
		}
		else
		{
			if(m_CurrentPawn != null)
			{
				m_CurrentPawn.EnablePlayerScale = false;
				m_CurrentPawn.EnablePlayerIcon = false;
				m_CurrentPawn.PlayerIconFlickerTimer = 0;
			}
		}
	}

	public void SetSquareData(PawnData i_PawnData)
	{
		m_CurrentPawn = i_PawnData;
	}

	public void SetGridPosition(int i_Position)
	{
		m_Position = i_Position;
	}
}
